using JetBrains.Annotations;
using Orchard.ContentManagement.Drivers;
using Orchard.CultureSwitcher.Models;
using Orchard.CultureSwitcher.Services;
using Orchard.Localization.Services;
using Orchard.ContentManagement;

namespace Orchard.CultureSwitcher.Drivers {
    [UsedImplicitly]
    public class CultureSwitcherPartDriver : ContentPartDriver<CultureSwitcherPart> {
        private readonly IOrchardServices _orchardServices;
        private readonly ICultureManager _cultureManager;
        private readonly IWorkContextAccessor _workContextAccessor;
        private readonly ISwitcherSettingsService _switcherSettingsService;

        public CultureSwitcherPartDriver(
            IOrchardServices orchardServices, 
            ICultureManager cultureManager, 
            IWorkContextAccessor workContextAccessor, 
            ISwitcherSettingsService switcherSettingsService) {
            _orchardServices = orchardServices;
            _cultureManager = cultureManager;
            _workContextAccessor = workContextAccessor;
            _switcherSettingsService = switcherSettingsService;
        }

        protected override DriverResult Display(CultureSwitcherPart part, string displayType, dynamic shapeHelper) {
            part.AvailableCultures = _switcherSettingsService.GetOrderedCulturesList();
            part.UserCulture = _cultureManager.GetCurrentCulture(_workContextAccessor.GetContext().HttpContext);

            var settings = _orchardServices.WorkContext.CurrentSite.As<SwitcherSettingsPart>();

            return ContentShape("Parts_CultureSwitcher", 
                () => shapeHelper.Parts_CultureSwitcher(
                    AvailableCultures: part.AvailableCultures, 
                    UserCulture: part.UserCulture,
                    Settings: settings
                )
            );
        }
    }
}